import json

from odoo import http
from ..functions.logger import log
from ..functions.rp_redis_utils import RedisUtils
from ..decorate.controller_exception_handler import PayException, exception_handler


class WebController(http.Controller):

    @http.route('/redis/get', type='http', auth='public', methods=['GET'])
    def available_error_data(self, redis_key, db=15):
        """
        获取Redis信息【H5通过这个接口获取到错误信息】
        Params:
        -redis_key:键
        """
        value = RedisUtils(db=db).get_str(redis_key.strip())
        if not redis_key or not value:
            return json.dumps({'status': 'error', 'msg': '传递的Session_key为空或值不存在', 'data': []})
        else:
            value = value.decode('utf-8')
            try:
                value = json.loads(value)
            except:
                value = value
            return json.dumps({'status': 'success', 'msg': '传递的Session_key为空或值不存在', 'data': value})

    @http.route('/test/pay_error', type='http', auth='public', method=['GET'])
    @exception_handler
    def test_pay_error(self):
        """
        测试异常信息
        """
        log('测试订单异常状态（订单已支付）')
        raise PayException('订单已支付')
